<?php

class Admin_Form_Agendamentonap extends Zend_Form
{
    public function init() {
   	    
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        $urlbase = $front->getBaseUrl();
        
        $id = $front->getRequest()->getParam('id', 0);

        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $this->setName('form_pdr');
        
        if($id > 0){
           $query = 'SELECT SQL_CACHE * FROM AGENDA_ATENDIMENTONAP WHERE AGA_ID = ' . $id;
           $data = $db->fetchRow($query);
        }
        
        Zend_Loader::loadClass('datetools','library');
        
        $el = new Zend_Form_Element_Hidden('AGA_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
       
        $arr = array(''=> 'Nenhum');
        $query = 'SELECT SQL_CACHE USU_ID,USU_NOME FROM USUARIOS WHERE PER_ID = 8 ORDER BY USU_NOME';
        $result = $db->fetchAll($query);
        foreach($result as $row) $arr[$row['USU_ID']] = $row['USU_NOME'];
        
        $el = new Zend_Form_Element_Select('USU_ID',array('onchange' => ''));
        $el->setLabel('Profissional')->setRequired(true);
        $el->addMultiOptions($arr);
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('AGA_DAT');
        $el->setLabel('Data')->setRequired(true);
        $el->setAttrib('class','data');
        $el->setAttrib('required','');
        $this->addElement($el);
        
        if($id == 0){
            
            $arr = array(''=> 'Nenhum');
            $el = new Zend_Form_Element_Select('AGA_HORINI');
            $el->setLabel('Horário')->setRequired(true);
            $el->addMultiOptions($arr);
            $el->setRegisterInArrayValidator(false);
            $el->setAttrib('required','');
            $this->addElement($el);
        }
        else{
            $html = '<dt><label>Horário</label></dt>';
            $html .= '<dd><input type="text"value="'.Datetools::hora_hm($data['AGA_HORINI']).' às '.Datetools::hora_hm($data['AGA_HORFIM']).'" readonly="" ></dd>';
                    
            $el = new Zend_Form_Element('html',array( 'helper'=>'formNote', 'value'=> $html ));
            $this->addElement($el);
        }
        
        
        $arr = array('0'=>'Nenhum');
        $query = ' SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM '
                .' FROM ALUNO a '
                .' INNER JOIN MATRICULA m ON m.ALU_ID = a.ALU_ID AND MAT_STA = 1 '
                .' INNER JOIN CURSO ON CURSO.CUR_ID = m.CUR_ID AND CUR_STA = 1 AND CUR_ANO = "'.date('Y-m-d').'"'
                .' WHERE a.ALU_STA = 1 ORDER BY a.ALU_NOM';
        $row = $db->fetchAll($query);
        foreach($row as $r) $arr[$r['ALU_ID']] = $r['ALU_NOM']; 
        
        $el = new Zend_Form_Element_Select('ALU_ID',array('onchange' => ''));
        $el->setLabel('Aluno')->setRequired(true)
        ->addMultiOptions($arr);
        $el->setAttrib('class','chosen-select');
        $el->setAttrib('required','');
        $this->addElement($el);

        $el = new Zend_Form_Element_Textarea('AGA_OBS');
        $el->setLabel('Observação');
        $el->setAttrib('class','txt_grd');
        $this->addElement($el);
        
        if($id > 0){
            $el = new Zend_Form_Element_Checkbox('AGA_STA',array('checked'=>'checked'));
            $el->setLabel('Status');
            $this->addElement($el);
        }
        
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Salvar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        
    }

}

